package com.mf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mf.entity.bo.BasicSocketBo;
import com.mf.mapper.BasicSocketMapper;
import com.mf.service.IBasicSocketService;
import com.mf.service.dto.WebSocketDto;
import io.github.heollhai.common.entity.request.SocketMessageRequest;
import io.github.heollhai.common.utils.VerifyUtils;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 服务实现类
 *
 * @author mf
 * @since 2024-10-17
 */
@Service
@Slf4j
public class BasicSocketServiceImpl extends ServiceImpl<BasicSocketMapper, BasicSocketBo> implements IBasicSocketService {
    @Resource
    private BasicSocketMapper basicSocketMapper;

    @Override
    public void sendMessage() {
        LambdaQueryWrapper<BasicSocketBo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BasicSocketBo::getStatus, 0);
        // 待发送的消息
        List<BasicSocketBo> basicSocketBos = basicSocketMapper.selectList(wrapper);

        if (VerifyUtils.isEmpty(basicSocketBos)) {
            log.info("【websocket消息】无待发送的消息=============no message await=====>>>>>>>>");
            return;
        }

        // 对没有发送的消息对在线用户进行发送
        for (BasicSocketBo basicSocketBo : basicSocketBos) {
            List<WebSocketDto> webSocketDtos = WebSocketServiceImpl.webSocketByUserIdMap.get(basicSocketBo.getUserId());
            SocketMessageRequest socketMessageRequest = new SocketMessageRequest();
            BeanUtils.copyProperties(basicSocketBo, socketMessageRequest);
            WebSocketServiceImpl.sendTextMessageByUserId(webSocketDtos, socketMessageRequest);
        }
    }
}
